import { MockMethod } from "vite-plugin-mock";

const userList = [
  {
    id: 1,
    username: "admin",
    email: "admin@example.com",
    avatar: "https://avatars.githubusercontent.com/u/1?v=4",
    roles: ["admin"],
  },
  {
    id: 2,
    username: "user",
    email: "user@example.com",
    avatar: "https://avatars.githubusercontent.com/u/2?v=4",
    roles: ["user"],
  },
];

export default [
  // 用户登录
  {
    url: "/api/auth/login",
    method: "post",
    response: ({ body }) => {
      const { username, password } = body;

      if (username === "admin" && password === "123456") {
        return {
          code: 200,
          message: "登录成功",
          data: {
            token: "mock-token-admin-" + Date.now(),
            userInfo: userList[0],
          },
        };
      } else if (username === "user" && password === "123456") {
        return {
          code: 200,
          message: "登录成功",
          data: {
            token: "mock-token-user-" + Date.now(),
            userInfo: userList[1],
          },
        };
      } else {
        return {
          code: 400,
          message: "用户名或密码错误",
          data: null,
        };
      }
    },
  },

  // 获取用户信息
  {
    url: "/api/auth/userinfo",
    method: "get",
    response: ({ headers }) => {
      const token = headers.authorization;

      if (token && token.includes("admin")) {
        return {
          code: 200,
          message: "获取成功",
          data: userList[0],
        };
      } else if (token && token.includes("user")) {
        return {
          code: 200,
          message: "获取成功",
          data: userList[1],
        };
      } else {
        return {
          code: 401,
          message: "未授权",
          data: null,
        };
      }
    },
  },

  // 用户登出
  {
    url: "/api/auth/logout",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "登出成功",
        data: null,
      };
    },
  },

  // 获取用户列表
  {
    url: "/api/users",
    method: "get",
    response: ({ query }) => {
      const { page = 1, pageSize = 10 } = query;
      const start = (page - 1) * pageSize;
      const end = start + pageSize;

      return {
        code: 200,
        message: "获取成功",
        data: {
          list: userList.slice(start, end),
          total: userList.length,
        },
      };
    },
  },
] as MockMethod[];
